package edu.xhu.homepage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xhu.homepage.mapper.BiliMainChannelMapper;
import edu.xhu.homepage.mapper.BiliSubChannelMapper;
import edu.xhu.homepage.service.BiliMainChannelService;
import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.model.common.enums.AppHttpCodeEnum;
import edu.xhu.model.video.pojos.BiliMainChannel;
import edu.xhu.model.video.pojos.BiliSubChannel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class BiliMainChannelServiceImpl extends ServiceImpl<BiliMainChannelMapper, BiliMainChannel> implements BiliMainChannelService {

    private final BiliSubChannelMapper biliSubChannelMapper;

    /**
     * 查询分区信息
     * 1.获取所有的主分区
     * 2.根据所有的主分区,获取所有的子分区
     * 3.返回
     */
    @Override
    public ResponseResult<?> queryChannelInfo() {
        //总的返回结果
        Map<String, Object> maps = new HashMap<>();
        List<BiliMainChannel> biliMainChannels = baseMapper.queryAllMainChannel();
        if (biliMainChannels == null || biliMainChannels.isEmpty()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        maps.put("biliMainChannels", biliMainChannels);
        //父子分区包一下
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> collect = biliMainChannels
                .stream()
                .map(biliMainChannel -> {
                    List<BiliSubChannel> biliSubChannels = biliSubChannelMapper.queryAllByMainChannelId(biliMainChannel.getMainChannelId());
                    //子分区可以无
                    map.put(biliMainChannel.getMainChannelName(), biliSubChannels);
                    return map;
                })
                .collect(Collectors.toList());
        maps.put("biliSubChannels", collect);
        return ResponseResult.okResult(maps);
    }
}
